package bkq.viettravel.bll;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class ImageCommon {
	
	private Context context;
	
	//khoi tao 2 biet width va height de thay doi kich thuoc  image 
	private int newWidth=0;
	private int newHeight=0;
	
	/**
	 * - Author :Danh
	 * - Describe:Contructor 
	 * @param context
	 * Context object for this
	 */
	public ImageCommon(Context context){
		this.context = context;
		
	}
	
	// khoi tao rong
	public ImageCommon(){
		
		
	}
	/**
	 * - Author :Hung
	 * - Describe:set output image size
	 * 
	 * dung ham nay de set kich thuoc anh can lay ra
	 */
	public void setImageSize(int imgWidth,int imgHeight){
		this.newWidth=imgWidth;
		this.newHeight=imgHeight;
	}
	/**
	 * Author :Dao Viet Danh --- Hung insert method resize image
	 * Describe : Get an image for web
	 * Date : 09/09/2011
	 * @param url
	 * url for your input
	 * @return
	 * Drawable 
	 * @throws Exception
	 */
	public Drawable downloadImage(String url) throws Exception{
				
		
		if(!(ConnectInternet.checkConnectInternet(this.context)))// if no connect
		return new BitmapDrawable(context.getAssets().open("img_default.jpg"));
		
		
		else{// connected 
			
			try {
				InputStream is = ConnectInternet.openHttpConnect(url);
				Drawable d = Drawable.createFromStream(is, "src");
				if(newWidth !=0 &&newHeight !=0)
				{
					
					d= reSize(d,newWidth,newHeight);
				}
				return d;
				
			} catch (MalformedURLException e) {
				e.printStackTrace();
				return null;
				
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}		
			
			
		}		
	}
	
	/**
	 * - Author :Hung
	 * - Describe:resize image download from internet
	 * 
	 * dung ham nay de set kich thuoc anh can lay ra
	 */
	 public Drawable reSize(Drawable d,int newWidth, int  newHeight){
		 Bitmap bitmapOrg = ((BitmapDrawable)d).getBitmap();
		 	int width = bitmapOrg.getWidth();
	        int height = bitmapOrg.getHeight();
	        
	        // calculate the scale - in this case = 0.4f
	        float scaleWidth = ((float) newWidth) / width;
	        float scaleHeight = ((float) newHeight) / height;
	        
	        // createa matrix for the manipulation
	        Matrix matrix = new Matrix();
	        // resize the bit map
	        matrix.postScale(scaleWidth, scaleHeight);
	        // rotate the Bitmap
	        //matrix.postRotate(45);

	        // recreate the new Bitmap
	        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true); 
	    
	       
	        // make a Drawable from Bitmap to allow to set the BitMap 
	        // to the ImageView, ImageButton or what ever
	        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
	        return bmd;
	 }
	 
	/**
	 * Author :Danh 
	 * Describe: get list image from list url
	 * Date : 09/009/2011
	 * @param url
	 * url your input
	 * @return
	 * List image
	 * @throws Exception
	 */
	public List<Drawable> getListImageFromListUrl(List<String> url) throws Exception{
		
		List<Drawable> setDrawable = new ArrayList<Drawable>();
		
		
		for(String item:url){
			
			setDrawable.add(downloadImage(item));
		}
		
		return (setDrawable == null) ? null : setDrawable;
		
	}

}
